package com.ocp.socket.busi.warnMsg.service.impl;

import com.ocp.socket.busi.warnMsg.bean.BaseWarnMsg;
import com.ocp.socket.busi.warnMsg.mapper.BaseWarnMsgMapper;
import com.ocp.socket.busi.warnMsg.service.IWarnMsgService;
import com.ocp.socket.common.enums.CacheKeyEnum;
import com.ocp.socket.common.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author : lichong
 * @description :
 * @Date Create in 20:57 2019/3/19
 * @Modified By :
 **/
@Service("warnMsgService")
public class WarnMsgServiceImpl implements IWarnMsgService {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private BaseWarnMsgMapper baseWarnMsgMapper;

    /**
     * 保存报警信息到数据库
     * @param warnMsgs
     */
    @Override
    public void saveWarnMsg(List<BaseWarnMsg> warnMsgs) {
        String key = null;
        //保存报警信息
        baseWarnMsgMapper.insertWarnMsgs(warnMsgs);
        for (BaseWarnMsg warnMsg : warnMsgs) {
            key = CacheKeyEnum.WARN_MSG_HAS_SENT.getCacheKey() + warnMsg.getSnNo();
            redisUtil.set(key, warnMsg.getId(), CacheKeyEnum.WARN_MSG_HAS_SENT.getExpireTime());
        }
    }

    /**
     * 修改预警信息的结束时间
     * @param snNo 设备no
     */
    @Override
    public void updateWarnMsgFinishTime(String snNo) {
        String key = CacheKeyEnum.WARN_MSG_HAS_SENT.getCacheKey() + snNo;
        String id = redisUtil.get(key, String.class);
        if (StringUtils.isNotEmpty(id)) {
            //修改过期时间
            baseWarnMsgMapper.updateWarnMsgEndTime(id);
            redisUtil.del(key);
        }
    }
}
